﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Practices.EnterpriseLibrary.Logging;
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;

namespace B2BCore
{
    /// <summary>
    /// Helper class for the Logging Application Block
    /// </summary>
    public class LogHelper
    {
        private static readonly object LockObject = new object();
        /// <summary>
        /// Writes a debug message to the log file under the debug category
        /// and with a severity level of verbose. This function does not log
        /// when the source is built in Release.
        /// </summary>
        /// <param name="message">The message to be logged</param>
        
        [Obsolete("Use normal log with verbose severity",true)]
        public static void DbgLog(String message)
        {
#if DEBUG
            DbgLog(new LogEntry() { Message = message });
#endif
        }

        /// <summary>
        /// Writes a debug message to the log file under the debug category
        /// and with a severity level of verbose. This function does not log
        /// when the source is built in Release.
        /// </summary>
        /// <param name="logEntry">A LogEntry object defining the properties of
        /// the debug message to be logged.</param>
        
        [Obsolete("Use normal log with verbose severity",true)]
        public static void DbgLog(LogEntry logEntry)
        {
#if DEBUG
            if (logEntry == null)
                throw new ArgumentNullException("logEntry", "logEntry is null.");

            lock (LockObject)
            {
                LogEntry dbgLogEntry = logEntry;

                dbgLogEntry.Categories.Add("Debug");
                dbgLogEntry.Severity = System.Diagnostics.TraceEventType.Verbose;

                try
                {
                    Logger.Write(dbgLogEntry);
                }
                catch (Exception e)
                {
                    bool rethrow = ExceptionPolicy.HandleException(e, "Exception Policy");
                    if (rethrow)
                        throw;
                }
            }
#endif
        }

        /// <summary>
        /// Writes a general message to the log file under the General category
        /// and with a severity level of informational.
        /// </summary>
        /// <param name="message">The message to be logged</param>
        public static void Log(String message)
        {
            Log(new LogEntry() { Message = message });        
        }

        /// <summary>
        /// Writes a general message to the log file under the General category
        /// and sets the severity as the user defindes
        /// </summary>
        /// <param name="message">The message to be logged</param>
        /// <param name="severity">The severity to be logged</param>
        public static void Log(String message, System.Diagnostics.TraceEventType severity)
        {
            Log(new LogEntry() { Message = message, Severity = severity });
        }


        /// <summary>
        /// Creates a new entry in the log.
        /// </summary>
        /// <param name="message">The message you want the log to have</param>
        /// <param name="severity">The severity you want the log to have</param>
        /// <param name="category">The catagory you want the log to have</param>
        public static void Log(String message, System.Diagnostics.TraceEventType severity, string category)
        {
            LogEntry log = new LogEntry();
            log.Message = message;
            if (severity.ToString() == null)
            {
                log.Severity = System.Diagnostics.TraceEventType.Information;
            }
            else
            {
                log.Severity = severity;
            }

            if (category == "" || category == null)
            {
                log.Categories.Add("General");
            }
            else
            {
                log.Categories.Add(category);
            }

            Log(log);
        }
        /// <summary>
        /// Writes a supplied LogEntry to the log file. If a category isn't assigned,
        /// the default General category is used. If a severity isn't assigned, a 
        /// default severity level if infromation is used.
        /// </summary>
        /// <param name="logEntry">A LogEntry object defining the properties of the message
        /// to be logged.</param>
        public static void Log(LogEntry logEntry)
        {
            if (logEntry == null)
                throw new ArgumentNullException("logEntry", "logEntry is null.");

            lock (LockObject)
            {
                LogEntry savedLogEntry = logEntry;

                // If there isn't a category assigned, assume it is just a general message
                if (savedLogEntry.Categories.Count == 0)
                    savedLogEntry.Categories.Add("General");

                // If there isn't a Severity assigned, assume its just informational
                if (savedLogEntry.Severity == 0)
                    savedLogEntry.Severity = System.Diagnostics.TraceEventType.Information;

                try
                {
                    // Log the entry
                    /*
                     * If severity = verbose only log if run in debug
                     */
                    if (savedLogEntry.Severity == System.Diagnostics.TraceEventType.Verbose)
                    {
#if DEBUG
                        Logger.Write(savedLogEntry);
#endif
                    }
                    else
                    {
                        Logger.Write(savedLogEntry);
                    }
                }
                catch (Exception e)
                {
                    bool rethrow = ExceptionPolicy.HandleException(e, "Exception Policy");
                    if (rethrow)
                        throw;
                }
            }
        }
    }
}
